//JavaScript代码区域
layui.use('element', function () {
    var element = layui.element, $ = layui.jquery;

    var arr = [];

    function createUrl(url) {
        return "<iframe src='" + url + "' style='width:100%;height:95%;border:none;'></iframe>";
    }

    //触发事件
    var active = {
        tabAdd: function () {
            //新增一个Tab项
            arr.push(this.innerText);
            element.tabAdd('demo', {
                title: this.innerText //用于演示
                , content: createUrl(this.href)
                , id: this.innerText //实际使用一般是规定好的id，这里以时间戳模拟下
            });
            element.tabChange('demo', this.innerText);
            $('#layChildren').children().height('100%');
        }
        , tabChange: function (othis) {
            //切换到指定Tab项

            element.tabChange('demo', this.innerText); //切换到：用户管理
            othis.addClass('layui-show');
        }
    };

    $('.menuA').on('click', function () {
        var othis = $(this), type = othis.data('type');
        var contentText = this.innerText;

        // alert(type);
        if (arr.length <= 0) {
            active[type] ? active[type].call(this, othis) : '';
        } else {
            if (arr.includes(contentText)) {
                active['tabChange'] ? active['tabChange'].call(this, othis) : '';
            } else {
                active[type] ? active[type].call(this, othis) : '';
            }
        }
        return false;
    });

    //Hash地址的定位
    var layid = location.hash.replace(/^#demo=/, '');
    element.tabChange('demo', layid);

    element.on('tab(demo)', function (elem) {
        location.hash = 'demo=' + $(this).attr('lay-id');
    });

    element.on('tabDelete(demo)', function (data) {
        arr.splice(data.index, 1);
    })

});